package cm.wc.Dao.Imple;

import cm.wc.Dao.BookDao;
import cm.wc.domain.Book;
import cm.wc.util.DateTimeUtil;
import cm.wc.util.JDBCUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:BookDaoImpl
 * @Author: 鹿建文
 * @Date: 2022/3/30  - 15:44
 * @Describe:
 */
public class BookDaoImpl implements BookDao {
    @Override
    public int insertBook(Book book) {
        return 0;
    }

    @Override
    public int updateBook(Book book) {
        return 0;
    }

    @Override
    public int deletetBook(Integer id) throws SQLException {
        Connection conn = JDBCUtil.getConnection();
        String sql = "delete from tbtest where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        int res = ps.executeUpdate();
        JDBCUtil.release(conn,ps,null);
        return res;
    }

    @Override
    public List<Book> selectAll() throws SQLException {
        ArrayList<Book> res = new ArrayList<>();
        Connection conn = JDBCUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select*from tbtest";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String author = rs.getString("author");
            Date birth1 = rs.getDate("birth");
            LocalDate birth = DateTimeUtil.toLocalDate(birth1);
            BigDecimal price = rs.getBigDecimal("price");
            Book book = new Book(id, name, author, birth, price);
            res.add(book);


        }
        JDBCUtil.release(conn, stmt, rs);
        return res;
    }

    @Override
    public List<Book> selectAll(Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public List<Book> selectBooksByName(String name) throws SQLException {
        ArrayList<Book> res = new ArrayList<>();
        Connection conn = JDBCUtil.getConnection();
        String sql = "select* from tbtest where name =?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            name = rs.getString("name");
            String author = rs.getString("author");
            Date birth = rs.getDate("birth");
            BigDecimal price = rs.getBigDecimal("price");
            Book book = new Book(id, name, author, DateTimeUtil.toLocalDate(birth), price);
            res.add(book);
        }
        JDBCUtil.release(conn,ps,rs);
        return res;
    }

    @Override
    public List<Book> selectBooksByNameLike(String name) throws SQLException {
        ArrayList<Book> res = new ArrayList<>();
        Connection conn = JDBCUtil.getConnection();
        String sql = "select* from tbtest where name like ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            name = rs.getString("name");
            String author = rs.getString("author");
            Date birth = rs.getDate("birth");
            BigDecimal price = rs.getBigDecimal("price");
            Book book = new Book(id, name, author, DateTimeUtil.toLocalDate(birth), price);
            res.add(book);
        }
        JDBCUtil.release(conn,ps,rs);
        return res;


    }

    @Override
    public   List<Book> selectBookById(Integer id) throws SQLException {
        ArrayList<Book> res = new ArrayList<>();
        Connection conn = JDBCUtil.getConnection();

        String sql = "select* from tbtest where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String name = rs.getString("name");
            String author = rs.getString("author");
            Date birth1 = rs.getDate("birth");
            LocalDate birth = DateTimeUtil.toLocalDate(birth1);
            BigDecimal price = rs.getBigDecimal("price");
            Book book = new Book(id, name, author, birth, price);
            res.add(book);
        }
        JDBCUtil.release(conn, ps, rs);
        return res;

    }
}
